﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Web.Utility;
using Portal.Systems.Model;
using Portal.Web.Model;

namespace Portal.Systems.Render
{
    public class UserRoleRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string HttpButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string RoleId = (string)WebEnvironments.Request(SiteId, "RoleId");
                RoleCls ORole = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateRoleModel(SiteId, RoleId);
                if (ORole == null) throw new Exception("Vị trí công việc không hợp lệ!");
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function NextAvailableRole(PageIndex)\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        RoleId=document.getElementById('txtRoleId').value;\r\n" +
                   "        AvailableKeyword=document.getElementById('txtAvailableKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRoleRenderBll.ServerSideReadingAvailableUser(SiteId,RoleId,AvailableKeyword,PageIndex).value;\r\n" +
                   "        document.getElementById('divAvailableRole').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+

                   "    function NextBelongRole(PageIndex)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        RoleId=document.getElementById('txtRoleId').value;\r\n" +
                   "        BelongKeyword=document.getElementById('txtBelongKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRoleRenderBll.ServerSideReadingBelongRole(SiteId,RoleId,BelongKeyword,PageIndex).value;\r\n" +
                   "        document.getElementById('divBelongRole').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "    function ClientAddUserToRole(UserId)\r\n"+
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        RoleId=document.getElementById('txtRoleId').value;\r\n"+
                   "        AvailableKeyword=document.getElementById('txtAvailableKeyword').value;\r\n" +
                   "        BelongKeyword=document.getElementById('txtBelongKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRoleRenderBll.ServerSideAddUserToRole(SiteId,RoleId,AvailableKeyword,BelongKeyword,UserId,0).value;\r\n" +
                   "        document.getElementById('divAvailableRole').innerHTML=AjaxOut.HtmlContent;\r\n"+
                   "        document.getElementById('divBelongRole').innerHTML=AjaxOut.RetExtraParam1;\r\n" +
                   "    }\r\n"+
                   "    function ClientRemoveUserFromRole(RoleId, UserId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        if(confirm('Bạn đã chắc chắn loại bỏ chưa?')==false)return;\r\n"+
                   "        RoleId=document.getElementById('txtRoleId').value;\r\n"+
                   "        AvailableKeyword=document.getElementById('txtAvailableKeyword').value;\r\n" +
                   "        BelongKeyword=document.getElementById('txtBelongKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRoleRenderBll.ServerSideRemoveUserFromRole(SiteId,RoleId,AvailableKeyword,BelongKeyword,UserId,0).value;\r\n" +
                   "        document.getElementById('divBelongRole').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divAvailableRole').innerHTML=AjaxOut.RetExtraParam1;\r\n" +
                   "    }\r\n"+
                   "</script>\r\n" +
                   "<input type=hidden id=txtRoleId value=\"" + RoleId + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   "   <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:window.open('RoleMan.aspx','_self');\"><img class=image src=\""+HttpButtonUrl+"/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <h3>Thiết lập cho nhóm: " + ORole.RoleName + "</div>\r\n" +
                   "  <div class=CssListForm>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=col1>\r\n" +
                   "            <div id=divAvailableRole>" + ServerSideReadingAvailableUser(SiteId, RoleId, "", 0).HtmlContent + "</div>\r\n" +
                   "        </td>\r\n" +
                   "        <td class=col2>\r\n" +
                   "         <div id=divBelongRole>" + ServerSideReadingBelongRole(SiteId,RoleId,"",0).HtmlContent+ "</div>\r\n" +
                   "        </td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   " </div>\r\n"+
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>"+ ex.Message.ToString()+"</div>\r\n";
            }
            
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingAvailableUser(string SiteId, string RoleId, string Keyword, int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                UserCls[] AvailableUsers = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAvailableUser(SiteId, RoleId, Keyword);
                ReturnPaging OReturnPaging = WebPaging.GetPaging(AvailableUsers.Length, CurrentPageIndex, 10, 8, "NextAvailableRole");
                string Html =
                    "<h3>Danh sách tài khoản</h3>\r\n"+
                    "<div class=CssFilter>Từ khóa: <input value=\""+Keyword+"\" type=textbox class=textbox id=txtAvailableKeyword><input type=button class=button value=\"Lọc\" onclick=\"javascript:NextAvailableRole(0);\"></div>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "    <tr>\r\n"+
                    "       <th class=thIndex>#</th>\r\n" +
                    "       <th class=thCode>Mã</th>\r\n" +
                    "       <th>Tài khoản</th>\r\n" +
                    "       <th></th>\r\n" +
                    "   </tr>\r\n" +
                    OReturnPaging.PagingText;
                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {
                    Html +=   " <tr>\r\n" +
                              "     <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                              "     <td>" + AvailableUsers[iIndex].LoginName + "</td>\r\n" +
                              "     <td>" + AvailableUsers[iIndex].FullName + "</td>\r\n" +
                              "     <td><input type=button class=button value=\"Đưa vào\" onclick=\"javascript:ClientAddUserToRole('"+AvailableUsers[iIndex].UserId+"');\"></td>\r\n" +
                              " </tr>\r\n";
                }

                Html +=
                    "</table>\r\n"+
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingBelongRole(string SiteId, string RoleId, string Keyword, int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                UserCls[] UsersBelongRole = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingUserBelongRole(SiteId, RoleId, Keyword);
                ReturnPaging OReturnPaging = WebPaging.GetPaging(UsersBelongRole.Length, CurrentPageIndex, 10, 8, "NextBelongRole");
                string Html =
                    "<h3>Danh sách tài khoản thuộc nhóm</h3>\r\n" +
                    "<div class=CssFilter>Từ khóa: <input value=\""+Keyword+"\" type=textbox class=textbox id=txtBelongKeyword><input type=button class=button value=\"Lọc\" onclick=\"javascript:NextBelongRole(0);\"></div>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "    <tr>\r\n" +
                    "       <th class=thIndex>#</th>\r\n" +
                    "       <th class=thCode>Mã</th>\r\n" +
                    "       <th>Tài khoản</th>\r\n" +
                    "       <th></th>\r\n" +
                    "   </tr>\r\n" +
                    OReturnPaging.PagingText;
                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {
                    Html += " <tr>\r\n" +
                              "     <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                              "     <td>" + UsersBelongRole[iIndex].LoginName + "</td>\r\n" +
                              "     <td>" + UsersBelongRole[iIndex].FullName + "</td>\r\n" +
                              "     <td><input type=button class=button value=\"Loại ra\" onclick=\"javascript:ClientRemoveUserFromRole('" +RoleId+"','"+ UsersBelongRole[iIndex].UserId + "');\"></td>\r\n" +
                              " </tr>\r\n";
                }

                Html +=
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideAddUserToRole(
            string SiteId, 
            string RoleId, 
            string KeywordAvailable,
            string KeywordBelong,
            string UserId,
            int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddUserToRole(null, SiteId, RoleId, UserId);
                OAjaxOut.HtmlContent = ServerSideReadingAvailableUser(SiteId,RoleId,KeywordAvailable,0).HtmlContent;
                OAjaxOut.RetExtraParam1 = ServerSideReadingBelongRole(SiteId, RoleId, KeywordBelong, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideRemoveUserFromRole(
            string SiteId, 
            string RoleId, 
            string KeywordAvailable,
            string KeywordBelong,
            string UserId,
            int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).RemoveUserFromRole(null, SiteId, RoleId, UserId);
                OAjaxOut.HtmlContent = ServerSideReadingBelongRole(SiteId,RoleId,KeywordAvailable,0).HtmlContent;
                OAjaxOut.RetExtraParam1 = ServerSideReadingAvailableUser(SiteId, RoleId, KeywordBelong, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
